package com.juzipi.rabbit.controller;

import com.juzipi.rabbit.service.MailService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Author juzipi
 * @Date 2021/7/17 15:13
 * @Info
 */
@RestController
public class RabbitController {


    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private MailService mailService;

//    @PostMapping("send")
//    public String sendDirectMessage(@RequestBody String message){
//        String s = String.valueOf(UUID.randomUUID());
//        String sendTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//        HashMap<String, Object> hashMap = new HashMap<>();
//        hashMap.put("msgId",s);
//        hashMap.put("msgData",message);
//        hashMap.put("sendTime",sendTime);
//        rabbitTemplate.convertAndSend("getDirectExchange","getDirectRouting",hashMap);
//        return "success";
//    }


    //死信队列，没看出有啥
//    @PostMapping("dead")
//    public String testDeadQueue(){
//        String s = String.valueOf(UUID.randomUUID());
//        String sendTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//        HashMap<String, Object> hashMap = new HashMap<>();
//        hashMap.put("msgId",s);
//        hashMap.put("msgData","测试死信队列");
//        hashMap.put("sendTime",sendTime);
//        rabbitTemplate.convertAndSend("TestDeadExchange","TestDeadRouting",hashMap);
//        return "success";
//    }


    @PostMapping("delay")
    public String testDelayQueue(){
        String s = String.valueOf(UUID.randomUUID());
        String sendTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("msgId",s);
        hashMap.put("msgData","测试延迟队列");
        hashMap.put("sendTime",sendTime);
        rabbitTemplate.convertAndSend("TestDelayExchange","TestDelayRouting",hashMap);
        return "success";
    }


    // 对消息设置过期时间，队列不用设置过期时间
    public void testDelayQueueWithMessage() throws Exception {
        String msgId = String.valueOf(UUID.randomUUID());
        String sendTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map = new HashMap<>(4);
        map.put("msgId", msgId);
        map.put("msgData", "test dead letter queue");
        map.put("sendTime", sendTime);
        rabbitTemplate.convertAndSend("TestDelayExchange", "TestDelayRouting", map, msg -> {
            msg.getMessageProperties().setExpiration("5000");
            return msg;
        });
    }


    @GetMapping("sen")
    public String senEmail(){
        mailService.sendDelay(10);
        return "发送成功";
    }


}
